﻿using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ManaSky.SSO.Models
{
    /// <summary>
    /// ** 描述：JWT Token类
    /// ** 作者：杨逢春
    /// </summary>
    public class JwtConfig
    {
        /// <summary>
        /// 密钥
        /// </summary>
        [JsonProperty("secretKey")]
        public string SecretKey { get; set; } = string.Empty;
        /// <summary>
        /// 发布者
        /// </summary>
        [JsonProperty("issuer")]
        public string Issuer { get; set; } = string.Empty;
        /// <summary>
        /// 接受者
        /// </summary>

        [JsonProperty("audience")]
        public string Audience { get; set; } = string.Empty;
        /// <summary>
        /// 过期时间（小时）
        /// </summary>

        [JsonProperty("accessExpiration")]
        public int AccessExpiration { get; set; } = 24;
        /// <summary>
        /// 缓冲时间（秒）
        /// </summary>
        [JsonProperty("clockSkewExpiration")]
        public int ClockSkewExpiration { get; set; } = 30;


        [JsonProperty("refreshExpiration")]
        public int RefreshExpiration { get; set; }

        /// <summary>
        /// 密钥Bytes
        /// </summary>
        private SecurityKey SigningKey => new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SecretKey));

        /// <summary>
        /// 加密后的密钥，使用HmacSha256加密
        /// </summary>
        public SigningCredentials SigningCredentials =>
            new SigningCredentials(SigningKey, SecurityAlgorithms.HmacSha256);

        /// <summary>
        /// 认证用的密钥
        /// </summary>
        public SymmetricSecurityKey SymmetricSecurityKey => new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SecretKey));
    }
}
